package com.springlearn.zhoufei.other.spring.demo4.autotationAop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

//切面
@Aspect
@Component//自动注入
public class HelloAOP3_ASPECT {

    //注解切点
    @Pointcut("@annotation(com.springlearn.zhoufei.other.spring.demo4.autotationAop.HelloAOP3_annotation)")
    private void pointcut1() {
    }
    
    @Before("pointcut1()")
    public void beforeAdvice() {
        System.out.println("beforeAdvice");
    }
    
    @After("pointcut1()")
    public void afterAdvice(JoinPoint joinPoint) {

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        HelloAOP3_annotation helloAOP3_annotation = method.getAnnotation(HelloAOP3_annotation.class);
        System.out.println("注解式拦截 " + helloAOP3_annotation.name()); //5

        System.out.println("afterAdvice");
    }
    
    @AfterReturning(pointcut = "pointcut1()", returning = "retVal")
    public void afterReturningAdvice(Object retVal) {
        System.out.println("afterReturningAdvice:" + retVal);
    }
    
    @AfterThrowing(pointcut = "pointcut1()", throwing = "ex")
    public void AfterThrowingAdvice(IllegalArgumentException ex) {
        System.out.println("AfterThrowingAdvice");
    }
}